const {getHouseOne, getHouseList, delHouse, updateHouse, createHouse, getHouseListMy,getHouseOneMy} = require('../service/house')
const {SuccessModel, ErrorModel} = require('../model/ResModel')
const {addRoleFailInfo} = require('../model/ErrorInfo')
const {flatData} = require('../utils/flatData')

const getHouse = async ({name = '', address = '', minRent, maxRent, areaId = [], isSchool, schoolId, status, createTime = [], pageIndex = 0, pageSize = 10}) => {
  if (typeof areaId === 'string') {
    areaId = [ areaId ]
  }

  if (minRent !== undefined) {
    minRent = +minRent
  }

  if (maxRent !== undefined) {
    maxRent = +maxRent
  }

  const result = await getHouseList({
    name,
    address,
    minRent,
    maxRent,
    areaId,
    isSchool,
    schoolId,
    status,
    createTime,
    pageIndex,
    pageSize
  })
  result.data = flatData(result.data)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const getHouseById = async (id) => {
  let result = await getHouseOne(+id)
  result = flatData(result)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const getHouseMyById = async (id) => {
  let result = await getHouseOneMy(+id)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const getHouseShops = async (ctx, {name = '', address = '', minRent, maxRent, areaId = [], createTime = [], pageIndex = 0, pageSize = 10}) => {
  if (typeof areaId === 'string') {
    areaId = [ areaId ]
  }

  if (minRent !== undefined) {
    minRent = +minRent
  }

  if (maxRent !== undefined) {
    maxRent = +maxRent
  }

  let isSchool = ctx.session.user.role.isSchool
  if (isSchool) {
    isSchool = undefined
  } else {
    isSchool = true
  }

  const result = await getHouseList({
    name,
    address,
    minRent,
    maxRent,
    areaId,
    isSchool,
    status: 2,
    createTime,
    pageIndex,
    pageSize
  })
  result.data = flatData(result.data)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const postHouse = async (ctx, {name, address, rent, imgs, description, areaId, isSchool, schoolId, status = 1}) => {
  if (!isSchool) {
    schoolId = null
  }

  const result = await createHouse({
    name,
    address,
    rent,
    areaId,
    imgs,
    description,
    uid: ctx.session.user.user.id,
    isSchool,
    schoolId,
    status
  })
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const patchHouse = async ({name, address, rent, imgs, description, areaId, isSchool, schoolId, id}) => {
  if (!isSchool) {
    schoolId = null
  }
  const result = await updateHouse({name, address, rent, imgs, description, areaId, isSchool, schoolId, id})
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const patchHouseStatus = async ({status, id}) => {
  const result = await updateHouse({status: +status, id})
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const deleteHouse = async (id) => {
  const result = await delHouse(id)
  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

const getHouseMy = async (ctx, {name = '', address = '', minRent, maxRent, areaId = [], isSchool, schoolId, status, createTime = [], pageIndex = 0, pageSize = 10}) => {
  if (typeof areaId === 'string') {
    areaId = [ areaId ]
  }

  if (minRent !== undefined) {
    minRent = +minRent
  }

  if (maxRent !== undefined) {
    maxRent = +maxRent
  }

  let result = await getHouseListMy({
    uid: ctx.session.user.user.id,
    name,
    address,
    minRent,
    maxRent,
    areaId,
    isSchool,
    schoolId,
    status,
    createTime,
    pageIndex,
    pageSize
  })

  result.data = flatData(result.data)

  return result ? new SuccessModel(result) : new ErrorModel(addRoleFailInfo)
}

module.exports = {
  getHouse,
  getHouseById,
  postHouse,
  patchHouse,
  deleteHouse,
  getHouseMy,
  patchHouseStatus,
  getHouseShops,
  getHouseMyById
}
